// packageK/order/order.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statistics: {},
    tabs: ['待结算', '已结算', '提现记录'],
    userList: [],
    active: 0,
    amountTotal: 0
  },

  /**
   * 获取订阅消息tmplIds
   */
  getTmplIds: function() {
    const urlStr = app.getNetAddresss('member.member.getMiniTemplateCorrespond')
    const _this = this
    wx.showLoading({ title: '正在获取tmplIds...'})
    app._getNetWork({
      url: urlStr,
      data: { small_type: 3 },
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          _this.wxSubscriptionMessage(resData.data)
        } else {
          app.tips(resData.msg || '数据获取失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 订阅提现消息(提现状态通知/提现成功通知)
   * @param {Array} ids 订阅消息的tmplIds
   */
  wxSubscriptionMessage: function(ids = []) {
    if (!ids.length) {
      return
    }
    const tmplIds = ids.map(item => {
      return item.template_id
    })
    const _this = this
    wx.requestSubscribeMessage({
      tmplIds,
      complete: function() {
        _this.gotoIncome()
      }
    })
  },

  /**
   * 跳转到提现页面
   */
  gotoIncome: function() {
    wx.navigateTo({
      url: '/packageA/member/withdrawal/withdrawal',
    })
  },
  
  /**
   * 获取列表
   * @param {Object} e 
   */
  getList: function(e) {
    const { detail } = e || { detail: 0 }
    let urlStr = ''
    let data = {}
    if (detail == 2) {
      // 提现记录
      urlStr = app.getNetAddresss('finance.withdraw.withdraw-log')
      data = { withdraw_type : 'Yunshop\\Commission\\models\\CommissionOrder' }
    } else {
      // 待结算&已结算
      urlStr = app.getNetAddresss('plugin.commission.api.commission.get-new-commission-list')
      if (detail == 0) {
        data = { commission_type: 1 }
      } else if(detail == 1) {
        data = { commission_type: 2 }
      }
    }
    const _this = this
    this.setData({
      userList: [],
      amountTotal: 0,
      active: detail
    })
    wx.showLoading({ title: '数据加载中...' })
    app._postNetWork({
      url: urlStr,
      data,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const resultList = resData.data.data
          const { user } = resData.data
          const list = resultList.map(item => {
            return {
              id: item.id,
              avatar: detail == 2 ? user.avatar : item.member.avatar,
              name: detail == 2 ? (user.username || user.nickname) : (item.member.username || item.member.nickname),
              date: item.created_at,
              amount: detail == 2 ? ('提现' + item.amounts) : ('+' + item.commission)
            }
          })
          _this.setData({
            userList: list,
            amountTotal: detail == 2 ? resData.data.amount_total : resData.data.commission_total
          })
        } else {
          app.tips(resData.msg || '好友列表获取失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 获取统计数据
   */
  getStatistics: function() {
    const urlStr = app.getNetAddresss('plugin.commission.api.commission.statistic')
    const _this = this
    wx.showLoading({ title: '数据加载中...' })
    app._getNetWork({
      url: urlStr,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          _this.setData({ statistics: resData.data})
        } else {
          app.tips(resData.msg || '统计数据获取失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStatistics()
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})